package google.android.ageofempires;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Driver;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

public class Unit {
	private int[][][] mArrayIdBitmap = new int[GlobalSetting.ACTION][GlobalSetting.DIRECTION][GlobalSetting.BITMAP];
	private int mImageIndex;
	private int mBloodIndex;
	private int mCurBloodIndex;
	private int mMoveIndex;
	private int mAttackIndex;
	private int mScopeAttack;
	private int mKind;
	private int mAction;
	private int mDirection;
	private int mIdleSouth;
	private int mIdleSouthWest;
	private int mIdleWest;
	private int mIdleNouthWest;
	private int mIdleNouth;
	private int mMoveSouth;
	private int mMoveSouthWest;
	private int mMoveWest;
	private int mMoveNouthWest;
	private int mMoveNouth;
	private int mAttackSouth;
	private int mAttackSouthWest;
	private int mAttackWest;
	private int mAttackNouthWest;
	private int mAttackNouth;
	private int mDeathSouth;
	private int mDeathSouthWest;
	private int mDeathWest;
	private int mDeathNouthWest;
	private int mDeathNouth;
	private float mX, mY;
	private Context mContext;
	
	/*
	 * Constructor
	 */
	public Unit(Context context, int kind, int action, int direction) throws XmlPullParserException, IOException {
		mContext = context;		
		mKind = kind;
		mAction = action;
		mDirection = direction;
		getUnitFromXMLFile();
//		setArrayBitmap();
	}	
	
	/*
	 * Get Unit form XML file
	 */
	private void getUnitFromXMLFile() throws XmlPullParserException, IOException {		
		Resources res = mContext.getResources();
		XmlResourceParser parser = null;		
		if(mKind == GlobalSetting.MALEHERO) {
			parser = res.getXml(R.xml.malehero);			
		}
		else if(mKind == GlobalSetting.FEMALEHERO) {
			parser = res.getXml(R.xml.femalehero);			
		}
		else if(mKind == GlobalSetting.MANGONEL) {
			parser = res.getXml(R.xml.mangonel);			
		}						
		parser.next();
		int eventType = parser.getEventType();
		int action = 0, direction = 0, index = 0;
		while(eventType != XmlPullParser.END_DOCUMENT) {
			String name = null;
			switch(eventType) {
			case XmlPullParser.START_DOCUMENT:				
				break;
			case XmlPullParser.START_TAG:
				name = parser.getName();
				if(name.equalsIgnoreCase("blood")) {
					int blood = Integer.valueOf(parser.nextText());
					mBloodIndex = blood;
					mCurBloodIndex = blood;
					break;
				}
				else if(name.equalsIgnoreCase("move")) {
					int move = Integer.valueOf(parser.nextText());
					mMoveIndex = move;
					break;
				}
				else if(name.equalsIgnoreCase("attack")) {
					int attack = Integer.valueOf(parser.nextText());
					mAttackIndex = attack;
					break;
				}
				else if(name.equalsIgnoreCase("scopeattack")) {
					int scopeattack = Integer.valueOf(parser.nextText());
					mScopeAttack = scopeattack;
					break;
				}
				else if(name.equalsIgnoreCase("image-idle-south")) {
					action = GlobalSetting.IDLE;
					direction = GlobalSetting.SOUTH;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-idle-southwest")) {
					action = GlobalSetting.IDLE;
					direction = GlobalSetting.SOUTH_WEST;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-idle-west")) {
					action = GlobalSetting.IDLE;
					direction = GlobalSetting.WEST;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-idle-nouthwest")) {
					action = GlobalSetting.IDLE;
					direction = GlobalSetting.NOUTH_WEST;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-idle-nouth")) {
					action = GlobalSetting.IDLE;
					direction = GlobalSetting.NOUTH;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-move-south")) {
					action = GlobalSetting.MOVE;
					direction = GlobalSetting.SOUTH;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-move-southwest")) {
					action = GlobalSetting.MOVE;
					direction = GlobalSetting.SOUTH_WEST;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-move-west")) {
					action = GlobalSetting.MOVE;
					direction = GlobalSetting.WEST;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-move-nouthwest")) {
					action = GlobalSetting.MOVE;
					direction = GlobalSetting.NOUTH_WEST;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-move-nouth")) {
					action = GlobalSetting.MOVE;
					direction = GlobalSetting.NOUTH;
					index = 0;
					break;
				}				
				else if(name.equalsIgnoreCase("image-attack-south")) {
					action = GlobalSetting.ATTACK;
					direction = GlobalSetting.SOUTH;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-attack-southwest")) {
					action = GlobalSetting.ATTACK;
					direction = GlobalSetting.SOUTH_WEST;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-attack-west")) {
					action = GlobalSetting.ATTACK;
					direction = GlobalSetting.WEST;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-attack-nouthwest")) {
					action = GlobalSetting.ATTACK;
					direction = GlobalSetting.NOUTH_WEST;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-attack-nouth")) {
					action = GlobalSetting.ATTACK;
					direction = GlobalSetting.NOUTH;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-death-south")) {
					action = GlobalSetting.DEATH;
					direction = GlobalSetting.SOUTH;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-death-southwest")) {
					action = GlobalSetting.DEATH;
					direction = GlobalSetting.SOUTH_WEST;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-death-west")) {
					action = GlobalSetting.DEATH;
					direction = GlobalSetting.WEST;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-death-nouthwest")) {
					action = GlobalSetting.DEATH;
					direction = GlobalSetting.NOUTH_WEST;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("image-death-nouth")) {
					action = GlobalSetting.DEATH;
					direction = GlobalSetting.NOUTH;
					index = 0;
					break;
				}
				else if(name.equalsIgnoreCase("item")) {					
					String filename = parser.nextText();
					int id = getID(filename);
					mArrayIdBitmap[action][direction][index++] = id;
					if(action == GlobalSetting.IDLE) {
						if(direction == GlobalSetting.SOUTH) {
							mIdleSouth = index;
							break;
						}
						else if(direction == GlobalSetting.SOUTH_WEST) {
							mIdleSouthWest = index;
							break;
						}
						else if(direction == GlobalSetting.WEST) {
							mIdleWest = index;
							break;
						}
						else if(direction == GlobalSetting.NOUTH_WEST) {
							mIdleNouthWest = index;
							break;
						}
						else if(direction == GlobalSetting.NOUTH) {
							mIdleNouth = index;
							break;
						}
					}
					else if(action == GlobalSetting.MOVE) {
						if(direction == GlobalSetting.SOUTH) {
							mMoveSouth = index;
							break;
						}
						else if(direction == GlobalSetting.SOUTH_WEST) {
							mMoveSouthWest = index;
							break;
						}
						else if(direction == GlobalSetting.WEST) {
							mMoveWest = index;
							break;
						}
						else if(direction == GlobalSetting.NOUTH_WEST) {
							mMoveNouthWest = index;
							break;
						}
						else if(direction == GlobalSetting.NOUTH) {
							mMoveNouth = index;
							break;
						}
					}
					else if(action == GlobalSetting.ATTACK) {
						if(direction == GlobalSetting.SOUTH) {
							mAttackSouth = index;
							break;
						}
						else if(direction == GlobalSetting.SOUTH_WEST) {
							mAttackSouthWest = index;
							break;
						}
						else if(direction == GlobalSetting.WEST) {
							mAttackWest = index;
							break;
						}
						else if(direction == GlobalSetting.NOUTH_WEST) {
							mAttackNouthWest = index;
							break;
						}
						else if(direction == GlobalSetting.NOUTH) {
							mAttackNouth = index;
							break;
						}
					}
					else if(action  == GlobalSetting.DEATH) {
						if(direction == GlobalSetting.SOUTH) {
							mDeathSouth = index;
							break;
						}
						else if(direction == GlobalSetting.SOUTH_WEST) {
							mDeathSouthWest = index;
							break;
						}
						else if(direction == GlobalSetting.WEST) {
							mDeathWest = index;
							break;
						}
						else if(direction == GlobalSetting.NOUTH_WEST) {
							mDeathNouthWest = index;
							break;
						}
						else if(direction == GlobalSetting.NOUTH) {
							mDeathNouth = index;
							break;
						}
					}
				}
				break;
			case XmlPullParser.END_TAG:
				break;
			}
			eventType = parser.next();
		}									
	}		
	
	/*
	 * Convert InputStream to String
	 */
	private static String convertStreamToString(InputStream is) {				
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int i = is.read();
			while(i != -1) {
				baos.write(i);
				i = is.read();
			}
			return baos.toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*
	 * Set ArrayBitmap from .txt file
	 */
	private void setArrayBitmap() {		
		try {			
			AssetManager am = mContext.getAssets();
			InputStream is = am.open("mangonel.txt");
			String read = convertStreamToString(is);
			String[] arryAction = read.split("\r\n");
			for(int i = 0; i < 1; i++) {
				for(int j = 0; j < 2; j++) {
					String[] arrayFileName = arryAction[j].split(" ");
					for(int k = 0; k < arrayFileName.length; k++) {	
						int id = getID(arrayFileName[k]);
						mArrayIdBitmap[i][j][k] = id;																		
					}
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	
	/*
	 * Get ID, argument is Filename
	 */
	private int getID(String filename) {
		int id = mContext.getResources().getIdentifier(filename, "drawable", "google.android.ageofempires");
//		filename = "google.android.ageofempires:" + filename;
//		int id = mContext.getResources().getIdentifier(filename, null, null);
		return id;
	}		
	
	/*
	 * Get Number Image of Action
	 */
	public int getNumImageOfAction() {		
		if(mAction == GlobalSetting.IDLE) {
			if(mDirection == GlobalSetting.SOUTH) {				
				return mIdleSouth;
			}
			else if(mDirection == GlobalSetting.SOUTH_WEST) {				
				return mIdleSouthWest;
			}
			else if(mDirection == GlobalSetting.WEST) {				
				return mIdleWest;
			}
			else if(mDirection == GlobalSetting.NOUTH_WEST) {				
				return mIdleNouthWest;
			}
			else if(mDirection == GlobalSetting.NOUTH) {			
				return mIdleNouth;
			}
		}
		else if(mAction == GlobalSetting.MOVE) {
			if(mDirection == GlobalSetting.SOUTH) {		
				return mMoveSouth;
			}
			else if(mDirection == GlobalSetting.SOUTH_WEST) {				
				return mMoveSouthWest;
			}
			else if(mDirection == GlobalSetting.WEST) {				
				return mMoveWest;
			}
			else if(mDirection == GlobalSetting.NOUTH_WEST) {				
				return mMoveNouthWest;
			}
			else if(mDirection == GlobalSetting.NOUTH) {				
				return mMoveNouth;
			}
		}
		else if(mAction == GlobalSetting.ATTACK) {
			if(mDirection == GlobalSetting.SOUTH) {			
				return mAttackSouth;
			}
			else if(mDirection == GlobalSetting.SOUTH_WEST) {			
				return mAttackSouthWest;
			}
			else if(mDirection == GlobalSetting.WEST) {			
				return mAttackWest;
			}
			else if(mDirection == GlobalSetting.NOUTH_WEST) {				
				return mAttackNouthWest;
			}
			else if(mDirection == GlobalSetting.NOUTH) {			
				return mAttackNouth;
			}
		}
		else if(mAction == GlobalSetting.DEATH) {
			if(mDirection == GlobalSetting.SOUTH) {			
				return mDeathSouth;
			}
			else if(mDirection == GlobalSetting.SOUTH_WEST) {			
				return mDeathSouthWest;
			}
			else if(mDirection == GlobalSetting.WEST) {				
				return mDeathWest;
			}
			else if(mDirection == GlobalSetting.NOUTH_WEST) {				
				return mDeathNouthWest;
			}
			else if(mDirection == GlobalSetting.NOUTH) {				
				return mDeathNouth;
			}
		}
		return 0;
	}
	
	/*
	 * Set Index Of Image
	 */
	public void setImageIndex(int index) {
		mImageIndex = index;
	}
	
	/*
	 * Get Index Of Image
	 */
	public int getImageIndex() {
		return mImageIndex;
	}
	
	/*
	 * Set Action for Unit 
	 */
	public void setAction(int action) {
		mAction = action;
	}
	
	/*
	 * Get Action of Unit
	 */
	public int getAction() {
		return mAction;
	}
	
	/*
	 * Set Direction for Unit 
	 */
	public void setDirection(int direction) {
		mDirection = direction;
	}
	
	/*
	 * Get Direction of Unit
	 */
	public int getDirection() {
		return mDirection;
	}
	
	/*
	 * Set Kind Unit. For example: femalehero, malehero, mangonel...
	 */
	public void setKind(int kind) {
		mKind = kind;
	}
	
	/*
	 * Get Kind Unit
	 */
	public int getKind() {
		return mKind;
	}
	
	/*
	 * Set BloodIndex for Unit
	 */
	public void setBloodIndex(int bloodindex) {
		mBloodIndex = bloodindex;
	}
	
	/*
	 * Get BloodIndex of Unit
	 */
	public int getBloodIndex() {
		return mBloodIndex;		
	}
	
	/*
	 * Set Current BloodIndex for Unit
	 */
	public void setCurBloodIndex(int curbloodindex) {
		mCurBloodIndex = curbloodindex;
	}
	
	/*
	 * Get Current BloodIndex of Unit
	 */
	public int getCurBloodIndex() {
		return mCurBloodIndex;
	}
	/*
	 * Set MoveIndex for Unit
	 */
	public void setMoveIndex(int moveindex) {
		mMoveIndex = moveindex;
	}
	
	/*
	 * Get MoveIndex of Unit
	 */
	public int getMoveIndex() {
		return mMoveIndex;
	}
	
	/*
	 * Set AttackIndex for Unit
	 */
	public void setAttackIndex(int attackindex) {
		mAttackIndex = attackindex;
	}	
	
	/*
	 * Set AttackIndex of Unit. Use subtract blood of Opponent Unit
	 */
	public int getAttackIndex() {
		return mAttackIndex;
	}
	
	/*
	 * Set ScopeAttack for Unit
	 */
	public void setScopeAttack(int scopeattack) {
		mScopeAttack = scopeattack;
	}
	
	/*
	 * Get ScopeAttack of Unit
	 */
	public int getScopeAttack() {
		return mScopeAttack;
	}
	
	/*	 
	 * Set x-coordinate (Left-Top) 
	 */
	public void setX(float x) {
		mX = x;	
	}
	
	/*
	 * Get x-coordinate (Left-Top)
	 */
	public float getX() {
		return mX;
	}
	
	/*
	 * Set y-coordinate (Left-Top)
	 */
	public void setY(float y) {
		mY = y;
	}		
	
	/*
	 * Get y-coordinate (Left-Top)
	 */
	public float getY() {
		return mY;
	}
	
	/*
	 * Check Unit is Seclect
	 */
	public boolean isSelect(float x, float y) {
		boolean flag = false;
		
		return flag;
	}				
	
	/*
	 * Check Unit is Attack
	 */
	public boolean isAttack(Unit unit) {	
		double distance = Math.sqrt(Math.pow(mX - unit.getX(), 2) + Math.pow(mY - unit.getY(), 2));
		if(distance <= (double)mScopeAttack) {			
			return true;
		}
		return false;
	}
	
	/*
	 * Subtract Blood OpponentUnit
	 */
	public void subtractBloodUnit(Unit unit) {		
		int curBlood = unit.getCurBloodIndex();
		unit.setCurBloodIndex(curBlood - mAttackIndex);	
	}
	
	/*
	 * Attack OpponentUnit
	 */
	public void attackUnit(Unit unit) {
		double alpha = Utility.getDegree(new Point((int)mX, (int)mY), new Point((int)unit.getX(), (int)unit.getY()));
//		if(isAttack(unit)) {
			// Attack
			mAction = GlobalSetting.ATTACK;
			// Subtraction Blood Of OpponentUnit
//			int curBlood = unit.getCurBloodIndex();
//			unit.setCurBloodIndex(curBlood - mAttackIndex);
			
			if(alpha >= 80 && alpha <= 100) {
				mDirection = GlobalSetting.SOUTH;
				return;
			}
			else if(alpha > 100 && alpha <= 170) {
				mDirection = GlobalSetting.SOUTH_WEST;
				return;
			}
			else if(alpha > 170 && alpha <= 190) {
				mDirection = GlobalSetting.WEST;
				return;
			}
			else if(alpha > 190 && alpha <= 260) {
				mDirection = GlobalSetting.NOUTH_WEST;
				return;
			}
			else if(alpha > 260 && alpha <= 280) {
				mDirection = GlobalSetting.NOUTH;
				return;
			}
//		}
	}
	
	/*
	 * Check Unit is Death: true-death, false-not death
	 */
	public boolean isDeath() {		
		if(mCurBloodIndex <= 0) {
			return true;
		}
		return false;
	}
	
	/*
	 * Unit Death
	 */
	public void death() {
		if(isDeath()) {
			mAction = GlobalSetting.DEATH;
		}
	}
	
	/*
	 * Get ID of Bitmap
	 */
	public int getIdBitmap(int index) {		
		return mArrayIdBitmap[mAction][mDirection][index];
	}
	
	/*
	 * Get Bitmap
	 */
	public Bitmap getBitmap(int index) {
		Bitmap bm = null;
		int id = mArrayIdBitmap[mAction][mDirection][index];
		bm = BitmapFactory.decodeResource(mContext.getResources(), id);
		return bm;
	}
	
	/*
	 * Draw Unit
	 */
	public void drawUnit(Canvas canvas, int x, int y, int indexImage, Paint paint) {
		Bitmap bm = this.getBitmap(indexImage);					
//		bm = Utility.Transparency(bm);
		
		int width = bm.getWidth();
		int height = bm.getHeight();
		
		int _x = x - width / 2;
		int _y = y - height / 2;		
		
		// draw shadow of Unit					
		canvas.drawBitmap(bm, _x, _y, null);					
		bm = this.getBitmap(indexImage + 1);					
//		bm = Utility.Transparency(bm);
		
		// draw Unit		
		canvas.drawBitmap(bm, _x, _y, null);
		
		//draw blood of Unit
		Paint _paint = new Paint();		
		_paint.setColor(Color.RED);
		_paint.setStrokeWidth(3);
		canvas.drawLine(_x + (width - mBloodIndex) / 2, y - 60, _x + (width - mBloodIndex) / 2 + mBloodIndex, y - 60, _paint);
		
		if(!isDeath()) {			
			canvas.drawLine(_x + (width - mBloodIndex) / 2, y - 60, _x + (width - mBloodIndex) / 2 + mCurBloodIndex, y - 60, paint);					
		}
	}
	
	/*
	 * Draw Unit
	 */
	public void drawUnit(Canvas canvas, int x, int y, int indexImage) {
		Bitmap bm = this.getBitmap(indexImage);					
		bm = Utility.Transparency(bm);
		
		int width = bm.getWidth();
		int height = bm.getHeight();
		
		int _x = x - width / 2;
		int _y = y - height / 2;		
		
		// draw shadow of Unit					
		canvas.drawBitmap(bm, _x, _y, null);					
		bm = this.getBitmap(indexImage + 1);					
		bm = Utility.Transparency(bm);
		
		// draw Unit		
		canvas.drawBitmap(bm, _x, _y, null);
		
		//draw blood of Unit
		Paint paint = new Paint();
		paint = new Paint();
		paint.setColor(Color.RED);
		paint.setStrokeWidth(3);
		canvas.drawLine(_x + (width - mBloodIndex) / 2, _y - 5, _x + (width - mBloodIndex) / 2 + mBloodIndex, _y - 5, paint);
		
		if(!isDeath()) {
			paint.setColor(Color.BLUE);					
			canvas.drawLine(_x + (width - mBloodIndex) / 2, _y - 5, _x + (width - mBloodIndex) / 2 + mCurBloodIndex, _y - 5, paint);					
		}		
	}
	
	/*
	 * Get ID of Action is Idle
	 */
	public int[] getIdle() {
		int[] mArrayID = new int[GlobalSetting.BITMAP];
		switch (mDirection) {
		case 0:
			for(int i = 0; i < GlobalSetting.BITMAP; i++) {
				if(mArrayIdBitmap[0][0][i] != 0)
				{
					mArrayID[i] = mArrayIdBitmap[0][0][i];
				}
			}
			break;			
		case 1:
			for(int i = 0; i < GlobalSetting.BITMAP; i++) {
				if(mArrayIdBitmap[0][0][i]!=0)
				{
					mArrayID[i] = mArrayIdBitmap[0][0][i];
				}
			}
			break;
		}
		return mArrayID;
	}	
}
